package org.yjc.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.yjc.entity.SysUser;
import org.yjc.repository.SysUserRepository;

@Component
public class UserUtils {
	@Autowired
	private SysUserRepository sysuserDAO;
	
	public SysUser getUser() {
		//SecurityContextHolder可以获取上下文对象
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//得到用户名
		String username = userDetails.getUsername();
		//找到唯一的对象
		SysUser sysUser = sysuserDAO.findByUsername(username);
		return sysUser;
		
	}
}
